package org.jeecg.common.util;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.vo.RequestVo;

import javax.servlet.http.HttpServletRequest;

@Slf4j
public class HttpServletRequestUtil {

    public static RequestVo getDeviceInfoVo(HttpServletRequest req) {
        String manufacturer = req.getHeader("manufacturer");            // 手机型号
        String system_version = req.getHeader("system-version");        //系统版本
        String cpuabi = req.getHeader("cpuabi");                        //cpu型号
        String networktype = req.getHeader("networktype");              //网络型号
        String versioncode = req.getHeader("versioncode");              //  版本号
        String deviceid = req.getHeader("deviceid");                    // 设备id
        String isRunningInVirtualSpace = req.getHeader("isRunningInVirtualSpace");                    // 是否开启分身
        String batteryLevel = req.getHeader("batteryLevel");                    // 电池电量
        String totalSize = req.getHeader("totalSize");                    // 设备容量大小（存储大小）
        String availableSize = req.getHeader("availableSize");                    // 设备容量大小（可用存储大小）
        String isAdbEnable = req.getHeader("isAdbEnable");                    // 是否开启USB调试
        String androidId = req.getHeader("androidId");                    // android id
        String hasSimCard = req.getHeader("hasSimCard");                    // sim卡状态
        String isDeveloperOptionsEnabled = req.getHeader("isDeveloperOptionsEnabled");                    // 是否开启开发者模式
        String model = req.getHeader("model");                    // 手机型号
        String isEmulator = req.getHeader("isEmulator");                    // 是否模拟器
        String storageNormal = req.getHeader("storageNormal");                    // 存储设备数量
        String hasVpn = req.getHeader("hasVpn");                            // VPN连接状态
        String ipAddr = IpUtils.getIpAddr(req);
        RequestVo vo = new RequestVo();
        vo.setCpuAbi(cpuabi);
        vo.setDeviceId(deviceid);
        vo.setManufacturer(manufacturer);
        vo.setNetworkType(networktype);
        vo.setSystemVersion(system_version);
        vo.setVersionCode(versioncode);
        vo.setRunningInVirtualSpace(isRunningInVirtualSpace);
        if (StringUtils.isNotEmpty(batteryLevel)) {
            vo.setBatteryLevel(Double.parseDouble(batteryLevel));
        }

        if (StringUtils.isNotEmpty(totalSize)) {
            vo.setCapacitySize(Double.parseDouble(totalSize) / 1024 / 1024);
        }
        if (StringUtils.isNotEmpty(availableSize)) {
            vo.setFreeSpace(Double.parseDouble(availableSize) / 1024 / 1024);
        }
        vo.setAdbStatus(isAdbEnable);
        vo.setAndroidId(androidId);
        vo.setSimStatus(hasSimCard);
        vo.setModel(model);
        if (StringUtils.isNotEmpty(storageNormal)) {
            vo.setCapacityNum("Y".equals(storageNormal.toUpperCase()) ? 1 : 0);
        }

        vo.setVpnStatus(hasVpn);
        vo.setSimulator(isEmulator);          // 是否模拟器
        vo.setIp(ipAddr);
        return vo;
    }
}
